package com.xxl.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * shiro三大核心：认证利与，安全管理器，过滤器
 * @author xxl
 * @date 2023/3/14
 */
@Configuration
public class ShiroConfig {
    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(UserRealm userRealm) {
        return new DefaultWebSecurityManager(userRealm);
    }
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        //拦截权限不够的人
        chainDefinition.addPathDefinition("/view/common.html/level1-1","perms[user:vip1]");
        chainDefinition.addPathDefinition("/view/common.html/level1-2","perms[user:vip2]");
        //拦截不是管理员的
        chainDefinition.addPathDefinition("/view/common.html/admin","roles[admin]");
        return chainDefinition;
    }

    /**
     * shiro整合thymeleaf的核心bean，这个bean能识别前端的shiro元素
     */
    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }
}
